package com.neusoft.neu24.nep.controller;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.neusoft.neu24.nep.entity.CommonResult;
import com.neusoft.neu24.nep.entity.HttpCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;


@Component
public class MyBlockExceptionHandler  implements BlockExceptionHandler {

    private static final CommonResult baseDtoResponse = CommonResult.filure(HttpCodeEnum.LIMIT_DOWNGRADED_RESPONSE);


    public MyBlockExceptionHandler() {
    }

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
        response.setStatus(HttpStatus.OK.value());
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        response.setHeader("Content-Type","application/json;charset=utf-8");
        new ObjectMapper().writeValue(response.getWriter(),baseDtoResponse);

    }
}
